package com.simplesoftwares.client.library.appstructure.tablescreen;

import java.util.ArrayList;
import java.util.Collections;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.simplesoftwares.client.library.HasTable;
import com.simplesoftwares.client.library.appskeleton.ProcessLevelBar;
import com.simplesoftwares.client.library.appstructure.SuperModel;
import com.simplesoftwares.client.library.appstructure.SuperTable;
import com.simplesoftwares.client.library.appstructure.UiScreen;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.client.library.libservice.GenricService;
import com.simplesoftwares.client.library.libservice.GenricServiceAsync;


/**
 * Creates a screen with table.Screen follows the data binding method as it can fill itself 
 * without presenter. That's good kamla but only saying data binding is enough as we are documenting code
 * not explaining data binding.
 * @author Kamala
 *
 * @param <T>
 */
public abstract class TableScreen<T> extends UiScreen<T> implements HasTable
{
	/**
	 * {@link SuperTable}table object for this view;
	 */
	protected SuperTable<T>superTable;
	/**
	 * Service used to provide data from server 
	 */
	protected GenricServiceAsync service=GWT.create(GenricService.class);
	
	/**
	 * Instantiates a new TableScreen
	 * @param superTable
	 */
	public TableScreen(SuperTable<T> superTable) {
		super();
		this.superTable = superTable;
		//Always be run after create Screen.
		if(processlevelBarNames!=null)
		{
			  processLevelBar= new ProcessLevelBar(5,processlevelBarNames);
			  processLevelBar.setVisibleFalse(false);
		}
	}
	
	public TableScreen() {
		super();
	
	}
	
	

/////////////////////////////////////////GUI CREATION///////////////////////////////////////////////
	
	/**
	 * adds the table on content and process level bar if it is not null
	 */
	@Override
	protected void createGui() {
		
		if(processLevelBar!=null)
			content.add(processLevelBar.content);
		content.add(superTable.content);
		
		applyStyle();
		
	}
//////////////////////////////////////////CSS STYLING///////////////////////////////////////////////
	/**
	 * applies the style on screen
	 */
	
	public void applyStyle() {
		//content.getElement().setId("serviceview");
	}

///////////////////////////////////ABSTRACT METHODS(OVERRIDEN)///////////////////////////////////////////////

	/**
	 * Retrives data from server depending upon the querry.
	 */
	@Override
	public void retriveTable(MyQuerry querry) {
		superTable.connectToLocal();
		service.getSearchResult(querry,new AsyncCallback<ArrayList<SuperModel>>() {
			
			@Override
			public void onSuccess(ArrayList<SuperModel> result) {
				System.out.println("Size of result is "+result.size());
				for(SuperModel model:result)
				{
					superTable.getListDataProvider().getList().add((T) model);
					
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
			caught.printStackTrace();
				
			}
		});
		
	}
	/**
	 * clears the table 
	 */
	@Override
	public void clear() {
		//No implementation needed
	}
	/**
	 * validates the table.The table validation is will always depend heavily on requiremnt.So we provided a
	 * default true
	 */

	
	@Override
	public boolean validate() {
		
		
		return true;
	}
	/**
	 * enable otr disable the screen.
	 * @param state if true enables the screen if false disable
	 */

	@Override
	public void setEnable(boolean state) {
		superTable.setEnable(state);		
	}



	public SuperTable<T> getSuperTable() {
		return superTable;
	}



	public void setSuperTable(SuperTable<T> superTable) {
		this.superTable = superTable;
	}
	
	
	@Override
	public void createScreen() {
		
		
	}

	@Override
	public void updateModel(T model) {
		
		
	}

	@Override
	public void updateView(T model) {
		
		
	}
	
	

}
